<template>
    <div>
        <h1 style="text-align: center;">用户添加</h1>
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="用户名">
                <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密  码">
                <el-input type="password" v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item label="邮  箱">
                <el-input type="email" v-model="form.email"></el-input>
            </el-form-item>
            <el-form-item label="手机号">
                <el-input type="tel" v-model="form.tel"></el-input>
            </el-form-item>
            <el-form-item label="角色">
                <el-select v-model="form.role" placeholder="请选择角色">
                    <el-option v-for="role in roles.data" :key="role._id" :label="role.name" :value="role._id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否启用">
                <el-switch v-model="form.enable" active-color="#13ce66" inactive-color="#ff4949">
                </el-switch>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">立即创建</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
import { roleList, adminAdd } from '@/request'
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()

let form = reactive({
    username: '',
    password: '',
    email: '',
    tel: '',
    enable: false,
    role: ''
})

let roles = reactive({data: []})

let onSubmit = async () => {
    let data = await adminAdd(form)
    let { error_code, msg } = data
    console.log(data);
    if (error_code === 200) {
        ElMessage({
            message: msg,
            duration: 1500,
            type: 'success',
            onClose: () => {
                router.push('/user/list')
            }
        })
    } else {
        ElMessage({
            message: msg,
            duration: 1500,
            type: 'error'
        })
    }
}
let getRoles = async () => {
    let data = await roleList()
    roles.data = data.data.rolelist
}

getRoles()

</script>

<style></style>
